package com.wc.算法基础课.E第五讲动态规划.线性DP.乌龟棋;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/3/27 9:58
 * @description https://www.acwing.com/problem/content/314/
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 50, M = 360;
    static int[] a = new int[M];
    static int[] b = new int[5];
    // f[i][j][k][l] 表示A用了i，B用了j，C用了k，D用了l所有最多的值
    static int[][][][] f = new int[N][N][N][N];
    static int n, m;

    public static void main(String[] args) {
        n = sc.nextInt();
        m = sc.nextInt();
        for (int i = 0; i < n; i++) a[i] = sc.nextInt();
        for (int i = 0; i < m; i++) b[sc.nextInt()]++;

        for (int A = 0; A <= b[1]; A++) {
            for (int B = 0; B <= b[2]; B++) {
                for (int C = 0; C <= b[3]; C++) {
                    for (int D = 0; D <= b[4]; D++) {
                        int score = a[A * 1 + B * 2 + C * 3 + D * 4];
                        int v = score;
                        if (A > 0) v = Math.max(v, f[A - 1][B][C][D] + score);
                        if (B > 0) v = Math.max(v, f[A][B - 1][C][D] + score);
                        if (C > 0) v = Math.max(v, f[A][B][C - 1][D] + score);
                        if (D > 0) v = Math.max(v, f[A][B][C][D - 1] + score);
                        f[A][B][C][D] = v;
                    }
                }
            }
        }
        System.out.println(f[b[1]][b[2]][b[3]][b[4]]);
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
